﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL.Business;

public partial class product_edit : System.Web.UI.Page
{
    BLL.Business.BookBLL bookBLL = new BookBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDataGrid();
        }
    }
    public void BindDataGrid()
    {
        try
        {
            DataSet ds = bookBLL.SelectAll();

            if ((ds == null) ||                                         // ds为空
               (ds.Tables.Count == 0) ||                                // ds中没有表
               (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))  // 有表 没有数据
            {
            }
            else
            {
                productList.DataSource = ds;
                productList.DataBind();

                LblCurrentIndex.Text = "第 " + (productList.PageIndex + 1).ToString() + " 页";
                LblPageCount.Text = "共 " + productList.PageCount.ToString() + " 页";
                LblRecordCount.Text = "总共 " + ds.Tables[0].Rows.Count.ToString() + " 条";
                if (ds.Tables[0].Rows.Count == 0)
                {
                    btnFirst.Visible = false;
                    btnPrev.Visible = false;
                    btnNext.Visible = false;
                    btnLast.Visible = false;

                    LblCurrentIndex.Visible = false;
                    LblPageCount.Visible = false;
                    LblRecordCount.Visible = false;
                }
                else if (productList.PageCount == 1)
                {
                    btnFirst.Visible = false;
                    btnPrev.Visible = false;
                    btnNext.Visible = false;
                    btnLast.Visible = false;
                }

                // 计算生成分页页码,分别为："首 页" "上一页" "下一页" "尾 页"
                btnFirst.CommandName = "1";
                btnPrev.CommandName = (productList.PageIndex == 0 ? "1" : productList.PageIndex.ToString());

                btnNext.CommandName = (productList.PageCount == 1 ? productList.PageCount.ToString() : (productList.PageIndex + 2).ToString());
                btnLast.CommandName = productList.PageCount.ToString();
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    // 分页按钮响应函数
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        productList.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
        BindDataGrid();
    }
    protected void productList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "deleteBook")
        {
            try
            {
                if (bookBLL.DeleteBook(Int32.Parse(e.CommandArgument.ToString())))
                { 
                    BindDataGrid();
                }
                else
                {
                    Response.Write("<script>alter('出现错误，请检查！')</script>");
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
            }
        }
    }
    protected void productList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果设置了固定行高，那么列宽就不管用了
        //如果只设置列宽，行高会根据内容自适应
        //for (int i = 0; i < e.Row.Cells.Count; i++)
        //{
        //    e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
        //    e.Row.Height = Unit.Pixel(18);
  
        //    e.Row.Cells[i].Style.Add("word-break", "break-all"); 
        //}    
    }
}
